package com.zbcn.combootes.user.service.impl;

import com.zbcn.combootes.user.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public void createIndex() {
        //创建索引
        try {
            XContentBuilder builder = XContentFactory.jsonBuilder()
                    .startObject()
                        .field("dynamic",true)
                        .startObject("properties")
                            .startObject("name")
                                .field("type","text")
                                .startObject("fields")
                                    .startObject("keyword")
                                         .field("type", "keyword")
                                    .endObject()
                                .endObject()
                            .endObject()
                            .startObject("address")
                                .field("type","text")
                                .startObject("fields")
                                    .startObject("keyword")
                                        .field("type","keyword")
                                    .endObject()
                                .endObject()
                            .endObject()
                            .startObject("remark")
                                .field("type","text")
                                .startObject("fields")
                                    .startObject("keyword")
                                        .field("type","keyword")
                                    .endObject()
                                .endObject()
                            .endObject()
                            .startObject("age")
                                .field("type","integer")
                            .endObject()
                            .startObject("salary")
                                .field("type","float")
                            .endObject()
                            .startObject("birthDate")
                                .field("type","date")
                                .field("format", "yyyy-MM-dd")
                            .endObject()
                            .startObject("createTime")
                                .field("type","date")
                            .endObject()
                        .endObject()
                    .endObject();
            //打印日志
            log.debug(Strings.toString(builder));
            // 创建索引配置信息，配置
            Settings settings = Settings.builder()
                    .put("index.number_of_shards", 1)
                    .put("index.number_of_replicas", 0)
                    .build();
            // 新建创建索引请求对象，然后设置索引类型（ES 7.0 将不存在索引类型）和 mapping 与 index 配置
            CreateIndexRequest request = new CreateIndexRequest("zbcn-user");
            request.settings(settings);
            request.mapping(builder);
            // RestHighLevelClient 执行创建索引;
            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
            // 判断是否创建成功
            boolean isCreated = createIndexResponse.isAcknowledged();
            log.info("是否创建成功：{}", isCreated);
        } catch (IOException e) {
            log.error("创建失败。", e);
        }
    }

    @Override
    public void deleteIndex() {
        try {
            // 新建删除索引请求对象
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("zbcn-user");
            // 执行删除索引
            AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            // 判断是否删除成功
            boolean siDeleted = delete  .isAcknowledged();
            log.info("是否删除成功：{}", siDeleted);
        } catch (IOException e) {
            log.error("删除失败。", e);
        }
    }
}
